package com.lcx.plugin.actions;

import com.intellij.database.dataSource.LocalDataSource;
import com.intellij.database.model.RawConnectionConfig;
import com.intellij.database.psi.DbNamespaceImpl;
import com.intellij.openapi.actionSystem.ActionGroup;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.lcx.plugin.dto.DbConfigDTO;
import com.lcx.plugin.tool.CacheDataUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * 操作按钮分组
 *
 * @author daxiong
 * @date 2023/6/21 下午3:41
 */
public class MainActionGroup extends ActionGroup {
    /**
     * 缓存数据工具类
     */
    private final CacheDataUtils cacheDataUtils = CacheDataUtils.getInstance();

    /**
     * 是否不存在子菜单
     */
    private boolean notShow;

    /**
     * 是否显示组件
     *
     * @return 是否隐藏
     */
    @Override
    public boolean hideIfNoVisibleChildren() {
        return this.notShow;
    }


    /**
     * 根据右键在不同的选项上展示不同的子菜单
     *
     * @param event 事件对象
     * @return 动作组
     */
    @NotNull
    @Override
    public AnAction[] getChildren(@Nullable AnActionEvent event) {
        // 获取当前项目
        Project project = getEventProject(event);
        if (project == null) {
            return getEmptyAnAction();
        }

        // 获取选中的PSI元素
        PsiElement psiElement = event.getData(LangDataKeys.PSI_ELEMENT);

        LocalDataSource localDataSource = null;
        if (psiElement instanceof DbNamespaceImpl) {
            RawConnectionConfig connectionConfig = ((DbNamespaceImpl) psiElement).getDataSource().getConnectionConfig();
            if (connectionConfig instanceof LocalDataSource) {
                localDataSource = (LocalDataSource) connectionConfig;
            }
        }
        if (localDataSource == null) {
            return getEmptyAnAction();
        }
        // 获取数据库配置
        DbConfigDTO dbConfig = getDbConfig(localDataSource);
        // 保存数据到缓存
        cacheDataUtils.setDbConfig(dbConfig);
        cacheDataUtils.setProject(project);
        this.notShow = false;
        return getMenuList();
    }

    private DbConfigDTO getDbConfig(LocalDataSource localDataSource) {
        DbConfigDTO dbConfig = new DbConfigDTO();
        dbConfig.setUrl(localDataSource.getUrl());
        dbConfig.setUser(localDataSource.getUsername());
        dbConfig.setDriver(localDataSource.getDriverClass());
        dbConfig.setDbType(localDataSource.getDbms().getName());
        return dbConfig;
    }

    /**
     * 初始化注册子菜单项目
     *
     * @return 子菜单数组
     */
    private AnAction[] getMenuList() {
        String mainActionId = "com.lcx.easy.db.action.generateStr";
        String dbSettingsId = "com.lcx.easy.db.action.db.settings";
        ActionManager actionManager = ActionManager.getInstance();
        // 代码生成菜单
        AnAction mainAction = actionManager.getAction(mainActionId);
        if (mainAction == null) {
            mainAction = new MainAction("Generate Sql String");
            actionManager.registerAction(mainActionId, mainAction);
        }
        // 代码生成菜单
        AnAction dbSettingAction = actionManager.getAction(dbSettingsId);
        if (dbSettingAction == null) {
            dbSettingAction = new DbSettingAction("Generate Table");
            actionManager.registerAction(dbSettingsId, dbSettingAction);
        }
        // 返回所有菜单
        return new AnAction[]{dbSettingAction, mainAction};
    }


    /**
     * 获取空菜单组
     *
     * @return 空菜单组
     */
    private AnAction[] getEmptyAnAction() {
        this.notShow = true;
        return AnAction.EMPTY_ARRAY;
    }
}
